import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:sinosheets/utils/bus_utils.dart';
import 'package:sinosheets/utils/storage/LSUtil.dart';
import 'package:sinosheets/utils/theme/style_utils.dart';

import 'logic.dart';

class AddCodePage extends StatefulWidget {
  const AddCodePage({Key? key}) : super(key: key);

  @override
  State<AddCodePage> createState() => _AddCodePageState();
}

class _AddCodePageState extends State<AddCodePage> {
  final AddCodeLogic logic = Get.put(AddCodeLogic());

  @override
  Widget build(BuildContext context) {
    return GetBuilder<AddCodeLogic>(builder: (get) {
      return Scaffold(
        extendBodyBehindAppBar: true,
        appBar: AppBar(
          backgroundColor: Colors.transparent,
          elevation: 0,
          leading: IconButton(
            icon: Icon(Icons.arrow_back_ios_new_rounded, color: Colors.white, size: 20.r),
            onPressed: () => Navigator.of(context).pop(),
          ),
          centerTitle: true,
          title: Text(
            'Set Promo Code',
            style: StyleUtils.ts_1f_20_600.copyWith(color: Colors.white),
          ),
        ),
        body: Container(
          width: double.infinity,
          height: double.infinity,
          decoration: BoxDecoration(
            gradient: LinearGradient(
              begin: Alignment.topCenter,
              end: Alignment.bottomCenter,
              colors: [
                ColorUtils.themeColor,
                ColorUtils.themeColor.withOpacity(0.8),
                ColorUtils.c_f8f8f8,
              ],
              stops: [0.0, 0.3, 0.8],
            ),
          ),
          child: ListView(
            padding: EdgeInsets.zero,
            children: [
              SizedBox(height: 120.r),
              // 标题
              Text(
                'Promo Code',
                style: StyleUtils.ts_ff_24_600.copyWith(
                  fontSize: 28.r,
                  fontWeight: FontWeight.bold,
                ),
                textAlign: TextAlign.center,
              ),

              SizedBox(height: 8.r),

              // 副标题
              Text(
                'Enter the promo code you obtained',
                style: StyleUtils.ts_ff_14_400.copyWith(
                  color: Colors.white.withValues(alpha: 0.9),
                ),
                textAlign: TextAlign.center,
              ),

              SizedBox(height: 32.r),

              // 主要内容卡片
              Container(
                margin: EdgeInsets.symmetric(horizontal: 20.r),
                padding: EdgeInsets.all(24.r),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(20.r),
                  boxShadow: [
                    BoxShadow(
                      color: Colors.black.withValues(alpha: 0.08),
                      blurRadius: 20.r,
                      offset: Offset(0, 8.r),
                    ),
                  ],
                ),
                child: Column(
                  children: [
                    // 如果设置了code，则显示code
                    if (LSUtil.getInviteCode().isNotEmpty) ...[
                      Container(
                        padding: EdgeInsets.symmetric(horizontal: 16.r, vertical: 12.r),
                        decoration: BoxDecoration(
                          gradient: LinearGradient(
                            colors: [
                              ColorUtils.c_fdf3ec,
                              ColorUtils.c_fff3e7,
                            ],
                          ),
                          borderRadius: BorderRadius.circular(12.r),
                          border: Border.all(
                            color: ColorUtils.themeColor.withValues(alpha: 0.2),
                            width: 1.r,
                          ),
                        ),
                        child: Row(
                          children: [
                            Icon(
                              Icons.check_circle_outline_rounded,
                              color: ColorUtils.themeColor,
                              size: 20.r,
                            ),
                            SizedBox(width: 8.r),
                            Expanded(
                              child: Text(
                                'Your Promo Code: ${LSUtil.getInviteCode()}',
                                style: StyleUtils.ts_theme_14_600,
                              ),
                            ),
                          ],
                        ),
                      ),
                      SizedBox(height: 20.r),
                    ],

                    // 输入框
                    Container(
                      decoration: BoxDecoration(
                        color: ColorUtils.c_f8f8f8,
                        borderRadius: BorderRadius.circular(16.r),
                        border: Border.all(
                          color: ColorUtils.c_dcdcdc,
                          width: 1.r,
                        ),
                      ),
                      child: TextField(
                        controller: get.inviteCodeController,
                        style: StyleUtils.ts_1f_16_500,
                        decoration: InputDecoration(
                          hintText: 'Enter the Promo Code you obtained',
                          hintStyle: StyleUtils.ts_99_12_400,
                          prefixIcon: Icon(
                            Icons.confirmation_number_rounded,
                            color: ColorUtils.themeColor,
                            size: 20.r,
                          ),
                          border: InputBorder.none,
                          contentPadding: EdgeInsets.symmetric(horizontal: 16.r, vertical: 16.r),
                        ),
                      ),
                    ),

                    SizedBox(height: 24.r),

                    // 确认按钮
                    Container(
                      width: double.infinity,
                      height: 48.r,
                      decoration: BoxDecoration(
                        gradient: LinearGradient(
                          colors: [
                            ColorUtils.themeColor,
                            ColorUtils.themeColor.withValues(alpha: 0.8),
                          ],
                        ),
                        borderRadius: BorderRadius.circular(16.r),
                        boxShadow: [
                          BoxShadow(
                            color: ColorUtils.themeColor.withValues(alpha: 0.3),
                            blurRadius: 12.r,
                            offset: Offset(0, 4.r),
                          ),
                        ],
                      ),
                      child: Material(
                        color: Colors.transparent,
                        child: InkWell(
                          borderRadius: BorderRadius.circular(16.r),
                          onTap: get.dealInviteCode,
                          child: Center(
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Icon(
                                  Icons.check_rounded,
                                  color: Colors.white,
                                  size: 20.r,
                                ),
                                SizedBox(width: 8.r),
                                Text(
                                  'Confirm',
                                  style: StyleUtils.ts_ff_16_500.copyWith(
                                    fontWeight: FontWeight.bold,
                                  ),
                                ),
                              ],
                            ),
                          ),
                        ),
                      ),
                    ),

                    // 如果设置了code，显示额外操作按钮
                    if (LSUtil.getInviteCode().isNotEmpty) ...[
                      SizedBox(height: 16.r),

                      // 清空按钮
                      Container(
                        width: double.infinity,
                        height: 44.r,
                        decoration: BoxDecoration(
                          color: Colors.red.withValues(alpha: 0.1),
                          borderRadius: BorderRadius.circular(12.r),
                          border: Border.all(
                            color: Colors.red.withValues(alpha: 0.3),
                            width: 1.r,
                          ),
                        ),
                        child: Material(
                          color: Colors.transparent,
                          child: InkWell(
                            borderRadius: BorderRadius.circular(12.r),
                            onTap: () {
                              LSUtil.saveInviteCode('', false);
                              get.inviteCodeController.clear();
                              get.update();
                              bus.emit('code_update');
                            },
                            child: Center(
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: [
                                  Icon(
                                    Icons.delete_outline_rounded,
                                    color: Colors.red,
                                    size: 18.r,
                                  ),
                                  SizedBox(width: 8.r),
                                  Text(
                                    'Clear Code',
                                    style: StyleUtils.ts_fd5155_14_500,
                                  ),
                                ],
                              ),
                            ),
                          ),
                        ),
                      ),

                      SizedBox(height: 16.r),

                      // 复制邀请链接按钮
                      Container(
                        width: double.infinity,
                        height: 44.r,
                        decoration: BoxDecoration(
                          gradient: LinearGradient(
                            colors: [
                              ColorUtils.c_40ba9d,
                              ColorUtils.c_40ba9d.withValues(alpha: 0.8),
                            ],
                          ),
                          borderRadius: BorderRadius.circular(12.r),
                          boxShadow: [
                            BoxShadow(
                              color: ColorUtils.c_40ba9d.withValues(alpha: 0.3),
                              blurRadius: 8.r,
                              offset: Offset(0, 2.r),
                            ),
                          ],
                        ),
                        child: Material(
                          color: Colors.transparent,
                          child: InkWell(
                            borderRadius: BorderRadius.circular(12.r),
                            onTap: () {
                              get.copyInviteLink();
                            },
                            child: Center(
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: [
                                  Icon(
                                    Icons.share_rounded,
                                    color: Colors.white,
                                    size: 18.r,
                                  ),
                                  SizedBox(width: 8.r),
                                  Text(
                                    'Copy Invite Link',
                                    style: StyleUtils.ts_ff_14_500.copyWith(
                                      fontWeight: FontWeight.bold,
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          ),
                        ),
                      ),
                    ],
                  ],
                ),
              ),

              SizedBox(height: 32.r),

              // 底部提示信息
              Container(
                margin: EdgeInsets.symmetric(horizontal: 20.r),
                padding: EdgeInsets.all(16.r),
                decoration: BoxDecoration(
                  color: Colors.white.withValues(alpha: 0.9),
                  borderRadius: BorderRadius.circular(12.r),
                ),
                child: Row(
                  children: [
                    Icon(
                      Icons.info_outline_rounded,
                      color: Colors.green.withValues(alpha: 0.8),
                      size: 20.r,
                    ),
                    SizedBox(width: 12.r),
                    Expanded(
                      child: Text(
                        'Promo codes can provide exclusive discounts and special offers for your purchases.',
                        style: StyleUtils.ts_99_12_400,
                      ),
                    ),
                  ],
                ),
              ),

              SizedBox(height: 40.r),
            ],
          ),
        ),
      );
    });
  }

  @override
  void dispose() {
    Get.delete<AddCodeLogic>();
    super.dispose();
  }
}
